package cn.shop.dys.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.markmao.pulltorefresh.widget.XListView;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.productdetail.PingLunRes;
import cn.shop.dys.bean.productdetail.PinglunInfo;
import cn.shop.dys.constant.CommonConstant;


/**
 * Created by zhouxiang on 2016/4/23.
 */
public class QFragment extends Fragment implements XListView.IXListViewListener {
    private XListView lv_qb;
    private List<PinglunInfo> comment;
    private HttpUtils httpUtils = new HttpUtils();
    private RequestParams params;
    private int pId;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmet_qubu, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv_qb = (XListView) view.findViewById(R.id.lv_qubu);
        lv_qb.setXListViewListener(this);
        lv_qb.setPullLoadEnable(true);

        lv_qb.setAdapter(adapter);
        Intent intent = getActivity().getIntent();
        pId = intent.getIntExtra("pId", 1);

        params = new RequestParams();
        params.addQueryStringParameter("pId",""+ pId);
        params.addQueryStringParameter("page", "0");
        params.addQueryStringParameter("pageNum", "10");
        httpUtils.send(HttpRequest.HttpMethod.GET, url, params, callback);
    }

    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return comment == null ? 0 : comment.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                ViewHolder holder = new ViewHolder();
                convertView = View.inflate(getActivity(), R.layout.item_xlistview, null);
                holder.nametV = (TextView) convertView.findViewById(R.id.tv_name);
                holder.neirongtv = (TextView) convertView.findViewById(R.id.tv_neirong);
                holder.timetv = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.nametV.setText(comment.get(position).username);
            holder.neirongtv.setText(comment.get(position).content);
            holder.timetv.setText("购买日期：" + comment.get(position).time);
            return convertView;
        }
    };
    private String url = CommonConstant.HOST + "/product/comment";

    @Override
    public void onRefresh() {
        httpUtils.send(HttpRequest.HttpMethod.GET, url, params, callback);
        Toast.makeText(getActivity(), "已经是最新的数据拉", Toast.LENGTH_SHORT).show();
        lv_qb.stopRefresh();

    }

    @Override
    public void onLoadMore() {
        httpUtils.send(HttpRequest.HttpMethod.GET, url, params, callback);
        lv_qb.stopLoadMore();
        Toast.makeText(getActivity(), "已经是最新的数据拉", Toast.LENGTH_SHORT).show();

    }

    private static class ViewHolder {
        TextView nametV;

        TextView neirongtv;

        TextView timetv;

    }

    private RequestCallBack<String> callback = new RequestCallBack<String>() {
        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            String json = responseInfo.result;
            PingLunRes pingLunRes = new Gson().fromJson(json, PingLunRes.class);
            comment = pingLunRes.comment;
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(HttpException e, String s) {

        }
    };
}
